﻿using System;
using System.Collections.Generic;
using System.Linq;
using HiStaff.Base;
using HiStaff.Control;
using HiStaff.Domain.HU;
using HiStaff.Util;
using HiStaff.Dal;
using System.Windows.Forms;
using Janus.Windows.GridEX;
using System.Data;

namespace HiStaff.iProfile
{
    public partial class Contract : FormBase
    {
        public Contract()
        {
            InitializeComponent();
        }

        protected override bool FormLoad()
        {
            try
            {
                CreateCommandBar(CommandBarManager.ActionCommand.New,
                CommandBarManager.ActionCommand.Edit,
                CommandBarManager.ActionCommand.Separator,
                CommandBarManager.ActionCommand.Save,
                CommandBarManager.ActionCommand.Cancel,
                CommandBarManager.ActionCommand.Separator,
                CommandBarManager.ActionCommand.Delete,
                CommandBarManager.ActionCommand.Separator,
                CommandBarManager.ActionCommand.Print,
                CommandBarManager.ActionCommand.Separator,
                CommandBarManager.ActionCommand.Exit);
                SetToolStripButtonBar(CommandBarManager.ActionCommand.Cancel);
                BaseStatus = Util.Enum.FormStatus.View;
                SetStatusInputControl(this);
                LoadCombo();
                return true;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }

        protected void LoadCombo()
        {
            try
            {
                Common.BindOrgSignContractToCombobox(cboContractOrg);
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }
        protected override bool PrintButtonClick()
        {
            try
            {
                string path;
                DataTable dt = new DataTable();
                dt = OracleHelper.ExcuteSelectDataTable("PRE_HU_CONTRACT", new EMPLOYEE_SEARCH { ID = ConvertHelper.ToDecimal(grdMain.GetValue("ID"))});
                path = (string)(dt.Rows[0]["TEMPLATE"]);
                Export.Export2WordFromDatabase(path, dt);
                return true;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }
        private void BindDataByEmp()
        {
            try
            {
                var lstEmployee = OracleHelper.ExcuteSelectMultiObject<HU_EMPLOYEE>("PRS_HU_EMPLOYEE", new EMPLOYEE_SEARCH { ORGID = trvOrg.OrgId, TERMINATE = hiCheckBox1.Value });
                grdEmpList.DataSource = lstEmployee;
                grdEmpList.Refresh();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void BindData()
        {
            try
            {
                var lstEmployee = OracleHelper.ExcuteSelectMultiObject<HU_CONTRACT>("APRS_HU_CONTRACT", new EMPLOYEE_SEARCH { EMPLOYEEID = ConvertHelper.ToDecimal(grdEmpList.GetValue("ID")) });
                grdMain.DataSource = lstEmployee;
                grdMain.Refresh();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void trvOrg_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                BindDataByEmp();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void grdEmpList_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                if (grdEmpList.GetValue("ID") == null)
                {
                    grdMain.DataSource = null;
                    grdMain.Refresh();
                    ResetInputControl(this);
                }
                else
                {
                    BindData();
                }
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void grdMain_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                BindDataFromGridToForm(grdMain, this);
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void hiCheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                BindDataByEmp();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        protected bool CheckData()
        {
            try
            {
                if (!Common.CheckEffectDateAndExpireDate(txtCEffectDate, txtExpireDate))
                {
                    MessageBox.Show(GetMessageById("MSG_INVALID_DATE"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }

        protected override bool SaveButtonClick()
        {
            try
            {
                if (!CheckData())
                {
                    return false;
                }
                else
                {
                    var objSave = new HU_CONTRACT();
                    ValueControlsToObject(objSave, this);
                    objSave.EMPLOYEEID = ConvertHelper.ToDecimal(grdEmpList.GetValue("ID"));
                    if (BaseStatus == Util.Enum.FormStatus.Add)
                    {
                        objSave.ID = 0;
                    }
                    OracleHelper.ExcuteNonQuery("APRU_HU_CONTRACT", objSave);

                    BindData();
                    MessageBox.Show(GetMessageById(Constant.MSG_TRANSACTION_SUCCESSFUL), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                return true;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }

        protected override bool CancelButtonClick()
        {
            try
            {
                BindDataFromGridToForm(grdMain, this);
                return true;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }

        protected override bool DeleteButtonClick()
        {
            if (grdMain.SelectedItems.Count > 0)
            {
                var row = grdMain.SelectedItems[0];
                if (row.RowType == Janus.Windows.GridEX.RowType.Record)
                {
                    if (OracleHelper.ExcuteNonQuery("APRD_HU_CONTRACT", row.GetRow().DataRow))
                    {
                        MessageBox.Show(GetMessageById(Constant.MSG_TRANSACTION_SUCCESSFUL), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        BindData();
                        return true;
                    }
                }
            }
            return false;
        }

        private void ResetExpireDate()
        {
            if (BaseStatus == Util.Enum.FormStatus.Add)
            {
                int m = Convert.ToInt16(((System.Data.DataRowView)(cboContractType.SelectedItem.DataRow)).Row.ItemArray[3]);
                if (m == 0 || txtCEffectDate.IsNullDate == true)
                {
                    txtExpireDate.IsNullDate = true;
                }
                else
                {
                    txtExpireDate.IsNullDate = false;
                    txtExpireDate.Value = (DateTime)(txtCEffectDate.Value).Value.AddMonths(m);
                }
            }
        }

        private void cboContractType_SelectedIndexChanged(object sender, EventArgs e)
        {

            try
            {
                ResetExpireDate();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void txtCEffectDate_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                txtCSignDate.Value = txtCEffectDate.Value;
                ResetExpireDate();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private string GetWorkingHour()
        {
            string a = "";
            if (BaseStatus == Util.Enum.FormStatus.Add)
            {
                var lstWorkingHour = OracleHelper.ExcuteSelectMultiObject<HU_CONTRACT>("PRS_HU_GETWORKINGHOUR", new EMPLOYEE_SEARCH { EMPLOYEEID = ConvertHelper.ToDecimal(grdEmpList.GetValue("ID")) });
                if (lstWorkingHour.Count > 0)
                {
                    a = lstWorkingHour[0].WORKINGTIME;
                }
                else { a = ""; }
            }
            return a;
        }

        protected override void ResetInputControl(System.Windows.Forms.Control container)
        {
            base.ResetInputControl(container);
            if (grdEmpList.SelectedItems.Count > 0 && grdEmpList.SelectedItems[0].RowType == RowType.Record)
            {
                hiSalarySelection1.EmployeeID = ((HU_EMPLOYEE)grdEmpList.SelectedItems[0].GetRow().DataRow).ID;
                txtWorkingHour.Text = GetWorkingHour();
            }
        }
    }
}
